//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.activiti.engine.impl.cmd;

import java.io.Serializable;
import java.util.Date;
import org.activiti.engine.ActivitiIllegalArgumentException;
import org.activiti.engine.delegate.event.ActivitiEventType;
import org.activiti.engine.delegate.event.impl.ActivitiEventBuilder;
import org.activiti.engine.impl.history.HistoryLevel;
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskInfo;
import org.apache.commons.lang3.StringUtils;

/**
 * 保存任务Cmd 继承与command 泛型指明了是对于 task的操作   要操作task需要创建TaskEntity将其实例化
 *   1.SaveTaskCmd 此类所需要对传入的task 要实现数据库，
 *   2.execute  获取命令的上下问 
 */
public class SaveTaskCmd implements Command<Task>, Serializable {
  private static final long serialVersionUID = 1L;
  protected TaskEntity task;

  public SaveTaskCmd(Task task) {
    this.task = (TaskEntity)task;
  }

  /**
   * 
   *  通过getTaskEntityManager  调取insert方法来插入数据库
   * @param commandContext
   * @return
   */

  public Task execute(CommandContext commandContext) {
    if (this.task == null) {
      throw new ActivitiIllegalArgumentException("task is null");
    } else if (this.task.getRevision() == 0) {
      commandContext.getTaskEntityManager().insert(this.task, (ExecutionEntity)null);
      //触发事件的调用
      if (commandContext.getEventDispatcher().isEnabled()) {
        commandContext.getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.TASK_CREATED, this.task));
        if (this.task.getAssignee() != null) {
          commandContext.getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.TASK_ASSIGNED, this.task));
        }
      }

      return null;
    } else {
      TaskInfo originalTaskEntity = null;
      if (commandContext.getProcessEngineConfiguration().getHistoryLevel().isAtLeast(HistoryLevel.AUDIT)) {
        originalTaskEntity = (TaskInfo)commandContext.getHistoricTaskInstanceEntityManager().findById(this.task.getId());
      }

      if (originalTaskEntity == null) {
        originalTaskEntity = (TaskInfo)commandContext.getTaskEntityManager().findById(this.task.getId());
      }

      String originalName = originalTaskEntity.getName();
      String originalAssignee = originalTaskEntity.getAssignee();
      String originalOwner = originalTaskEntity.getOwner();
      String originalDescription = originalTaskEntity.getDescription();
      Date originalDueDate = originalTaskEntity.getDueDate();
      int originalPriority = originalTaskEntity.getPriority();
      String originalCategory = originalTaskEntity.getCategory();
      String originalFormKey = originalTaskEntity.getFormKey();
      String originalParentTaskId = originalTaskEntity.getParentTaskId();
      String originalTaskDefinitionKey = originalTaskEntity.getTaskDefinitionKey();
      if (commandContext.getProcessEngineConfiguration().getHistoryLevel().isAtLeast(HistoryLevel.AUDIT)) {
        if (!StringUtils.equals(originalName, this.task.getName())) {
          commandContext.getHistoryManager().recordTaskNameChange(this.task.getId(), this.task.getName());
        }

        if (!StringUtils.equals(originalDescription, this.task.getDescription())) {
          commandContext.getHistoryManager().recordTaskDescriptionChange(this.task.getId(), this.task.getDescription());
        }

        if (originalDueDate == null && this.task.getDueDate() != null || originalDueDate != null && this.task.getDueDate() == null || originalDueDate != null && !originalDueDate.equals(this.task.getDueDate())) {
          commandContext.getHistoryManager().recordTaskDueDateChange(this.task.getId(), this.task.getDueDate());
        }

        if (originalPriority != this.task.getPriority()) {
          commandContext.getHistoryManager().recordTaskPriorityChange(this.task.getId(), this.task.getPriority());
        }

        if (!StringUtils.equals(originalCategory, this.task.getCategory())) {
          commandContext.getHistoryManager().recordTaskCategoryChange(this.task.getId(), this.task.getCategory());
        }

        if (!StringUtils.equals(originalFormKey, this.task.getFormKey())) {
          commandContext.getHistoryManager().recordTaskFormKeyChange(this.task.getId(), this.task.getFormKey());
        }

        if (!StringUtils.equals(originalParentTaskId, this.task.getParentTaskId())) {
          commandContext.getHistoryManager().recordTaskParentTaskIdChange(this.task.getId(), this.task.getParentTaskId());
        }

        if (!StringUtils.equals(originalTaskDefinitionKey, this.task.getTaskDefinitionKey())) {
          commandContext.getHistoryManager().recordTaskDefinitionKeyChange(this.task.getId(), this.task.getTaskDefinitionKey());
        }
      }

      if (!StringUtils.equals(originalOwner, this.task.getOwner())) {
        if (this.task.getProcessInstanceId() != null) {
          commandContext.getIdentityLinkEntityManager().involveUser(this.task.getProcessInstance(), this.task.getOwner(), "participant");
        }

        commandContext.getHistoryManager().recordTaskOwnerChange(this.task.getId(), this.task.getOwner());
      }

      if (!StringUtils.equals(originalAssignee, this.task.getAssignee())) {
        if (this.task.getProcessInstanceId() != null) {
          commandContext.getIdentityLinkEntityManager().involveUser(this.task.getProcessInstance(), this.task.getAssignee(), "participant");
        }

        commandContext.getHistoryManager().recordTaskAssigneeChange(this.task.getId(), this.task.getAssignee());
        commandContext.getProcessEngineConfiguration().getListenerNotificationHelper().executeTaskListeners(this.task, "assignment");
        commandContext.getHistoryManager().recordTaskAssignment(this.task);
        if (commandContext.getEventDispatcher().isEnabled()) {
          commandContext.getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createEntityEvent(ActivitiEventType.TASK_ASSIGNED, this.task));
        }
      }

      return (Task)commandContext.getTaskEntityManager().update(this.task);
    }
  }
}
